<?php
/*****************************************************************************
**	Класс:	Работа с шаблонами												**
**	Автор:	Dark Dayver														**
**	Версия:	1.0b															**
**	Дата:	25/12-2009														**
*****************************************************************************/
if (IN_P_ENIGMA)
{
	die('HACK!');
}

class template
{
	var $template_file = 'main.html'; // Файл шаблона
	var $array_data = array(); // Массив данных для замены
	var $ins_header = ''; // Данные, вставляемые в заголовок
	var $ins_body = ''; // Данные, вставляемые в тег body
	var $themes_path; // Путь к корню темы
	var $faction_path = false; // Путь к фракции
	var $themes_url; // Ссылка на корень темы
	var $faction_url = false; // Ссылка на корень фракции
	var $content; // Содержимое для вывода
	var $site_url; //Ссылка корня сайта
	var $site_dir; // Путь к корню сайта
	var $theme; // Тема пользователя
	var $faction; // Фракция пользователя

	function template($site_url, $site_dir, $theme, $faction)
	{
		$this->site_url = $site_url;
		$this->site_dir = $site_dir;
		$this->theme = $theme;
		$this->faction = $faction;
		$this->themes_path = $this->site_dir . 'themes/' . $this->theme . '/';
		$this->themes_url = $this->site_url . 'themes/' . $this->theme . '/';
		if ($faction)
		{
			$this->faction_path = $this->themes_path . $this->faction . '/';
			$this->faction_url = $this->themes_url . $this->faction . '/';
		}
	}

	function create_template()
	{
		$this->content = file_get_contents($this->themes_path . $this->template_file);
		if (!$this->content) die ('Error template file!');

		$this->pars_template();

		$this->content = str_replace('{THEME_URL}', $this->themes_url, $this->content);
		$this->content = str_replace('{FACTION_URL}', $this->faction_url, $this->content);

		if (get_magic_quotes_gpc())
		{
			$this->content = stripslashes($this->content);
		}
	}

	function pars_template()
	{
		while (list($key, $val) = each($this->array_data))
		{
			if (is_array($val))
			{
				$this->template_array($key, $val);
			}
			elseif (substr($key, 0, 3) == 'IF_')
			{
				$this->template_if($key, $val);
			}
			else
			{
				$this->content = str_replace('{' . $key . '}', $val, $this->content);
			}
		}
		$this->content = str_replace(chr(13) . chr(10), '{BR}', $this->content);
		$this->content = str_replace(chr(13), '{BR}', $this->content);
		$this->content = str_replace(chr(10), '{BR}', $this->content);
		while (strpos($this->content, '{BR}{BR}'))
		{
			$this->content = str_replace('{BR}{BR}', '{BR}', $this->content);
		}
		$this->content = str_replace('{BR}', PHP_EOL, $this->content);
	}

	private function template_if($key, $val)
	{
		while (strpos($this->content, '<!-- ' . $key . '_BEGIN -->'))
		{
			$start = strpos($this->content, '<!-- ' . $key . '_BEGIN -->') + strlen('<!-- ' . $key . '_BEGIN -->');
			if (strpos($this->content, '<!-- ' . $key . '_BEGIN -->') !== false && (strpos($this->content, '<!-- ' . $key . '_ELSE -->', $start) === false || strpos($this->content, '<!-- ' . $key . '_ELSE -->', $start) > strpos($this->content, '<!-- ' . $key . '_END -->', $start)))
			{
				if (!strpos($this->content, '<!-- ' . $key . '_END -->', $start)) die ('Error template IF "' . $key . '"!');
				$length = strpos($this->content, '<!-- ' . $key . '_END -->', $start) - $start;
				$temp_content = '<!-- ' . $key . '_BEGIN -->' . substr($this->content, $start, $length) . '<!-- ' . $key . '_END -->';
				if ($val == false)
				{
					$this->content = str_replace($temp_content, '', $this->content);
				}
				else
				{
					$tmp = substr($this->content, $start, $length);
					$this->content = str_replace($temp_content, $tmp, $this->content);
				}
			}
			else
			{
				if (!strpos($this->content, '<!-- ' . $key . '_END -->', $start_else)) die ('Error template IF "' . $key . '"!');
				$length = strpos($this->content, '<!-- ' . $key . '_END -->', $start) - $start;
				$temp_content = '<!-- ' . $key . '_BEGIN -->' . substr($this->content, $start, $length) . '<!-- ' . $key . '_END -->';
				$start_else = strpos($temp_content, '<!-- ' . $key . '_ELSE -->');
				if ($val == false)
				{
					$temp_lenght = strpos($temp_content, '<!-- ' . $key . '_ELSE -->') - strlen('<!-- ' . $key . '_BEGIN -->');
					$tmp = '<!-- ' . $key . '_BEGIN -->' . substr($temp_content, strlen('<!-- ' . $key . '_BEGIN -->'), $temp_lenght) . '<!-- ' . $key . '_ELSE -->';
					$temp_content_new = str_replace($tmp, '', $temp_content);
					$temp_content_new = str_replace('<!-- ' . $key . '_END -->', '', $temp_content_new);
				}
				else
				{
					$temp_lenght = strpos($temp_content, '<!-- ' . $key . '_END -->', $start_else) - $start_else;
					$tmp = substr($temp_content, $start_else, $temp_lenght) . '<!-- ' . $key . '_END -->';
					$temp_content_new = str_replace($tmp, '', $temp_content);
					$temp_content_new = str_replace('<!-- ' . $key . '_BEGIN -->', '', $temp_content_new);
				}
				$this->content = str_replace($temp_content, $temp_content_new, $this->content);
			}
		}
	}

	private function template_array($key, $val)
	{
		$start = strpos($this->content, '<!-- ' . $key . '_BEGIN -->') + strlen('<!-- ' . $key . '_BEGIN -->');
		if (!strpos($this->content, '<!-- ' . $key . '_END -->', $start)) die ('Error template ARRAY "' . $key . '"!');
		$length = strpos($this->content, '<!-- ' . $key . '_END -->', $start) - $start;
		$temp_template = new template($this->site_url, $this->site_dir, $this->theme, $this->faction);
		$tmp = substr($this->content, $start, $length);
		$block_content = '';

		while (list($array_count, $array_val) = each($val))
		{
			$temp_template->content = $tmp;
			$temp_template->array_data = $array_val;
			$temp_template->pars_template();
			$block_content .= $temp_template->content;
		}
		unset($temp_template);
		$this->content = str_replace('<!-- ' . $key . '_BEGIN -->' . $tmp . '<!-- ' . $key . '_END -->', $block_content, $this->content);
	}

	function page_header($title, $menu = false)
	{
		$temp_template = new template($this->site_url, $this->site_dir, $this->theme, $this->faction);
		$temp_template->array_data['TITLE'] = $title;
		$temp_template->array_data['INSERT_HEADER'] = $this->ins_header;
		if ($this->ins_body != '') $this->ins_body = ' ' . $this->ins_body;
		$temp_template->array_data['INSERT_BODY'] = $this->ins_body;
		if ($menu && is_array($menu))
		{
			$temp_template->array_data['ARRAY_MENU'] = $menu;
		}
		$temp_template->template_file = 'header.html';
		$temp_template->create_template();
		$this->content = $temp_template->content . $this->content;
		unset($temp_template);
	}

	function page_footer($debug)
	{
		global $language;

		$temp_template = new template($this->site_url, $this->site_dir, $this->theme, $this->faction);
		$temp_template->template_file = 'footer.html';
		$temp_template->array_data['IF_DEBUG'] = false;
		$temp_template->array_data['IF_ERROR'] = false;
		$temp_template->array_data['IF_INFO'] = false;
		if ($debug)
		{
			$temp_template->array_data['IF_DEBUG'] = true;
			$temp_template->array_data['D_TEXT_DEBUG'] = $debug;
			$temp_template->array_data['L_DEBUG_TITLE'] = $language->main['debug_title'];
		}
		if (isset($_SESSION['error']) && $_SESSION['error'] === true)
		{
			$temp_template->array_data['IF_ERROR'] = true;
			$temp_template->array_data['L_ERROR_TITLE'] = $language->main['error_title'];
			if (isset($_SESSION['error_txt']) && !empty($_SESSION['error_txt']))
			{
				$temp_template->array_data['D_TEXT_ERROR'] = $_SESSION['error_txt'];
			}
			else
			{
				$temp_template->array_data['D_TEXT_ERROR'] = $language->error['unknown'];
			}
			unset($_SESSION['error'], $_SESSION['error_txt']);
		}
		if (isset($_SESSION['info']) && $_SESSION['info'] === true)
		{
			$temp_template->array_data['IF_INFO'] = true;
			$temp_template->array_data['L_INFO_TITLE'] = $language->main['info_title'];
			if (isset($_SESSION['info_txt']) && !empty($_SESSION['info_txt']))
			{
				$temp_template->array_data['D_TEXT_INFO'] = $_SESSION['info_txt'];
			}
			else
			{
				$temp_template->array_data['D_TEXT_INFO'] = $language->error['unknown'];
			}
			unset($_SESSION['info'], $_SESSION['info_txt']);
		}
		$temp_template->create_template();
		$this->content = $this->content . $temp_template->content;
		unset($temp_template);
	}
}
?>
